<?php
class ControllerUDEMCommonImageIO extends Controller{
	public function getImage(){
		if(isset($this->request->get['path'])){
			$path = DIR_IMAGE . $this->request->get['path'];
			$file = fopen($path, 'r');
			
			$this->response->addHeaderNow('Content-Type:image/jpeg');
			echo fread($file, filesize($path));
			
			fclose($file);
			
		} else {
			echo $this->error->normalError('no path found!');
		}
		
		
	}
	
	public function uploadImages(){
		if($_SERVER['REQUEST_METHOD'] = 'POST'){
			var_dump($_FILES);
			
			if(empty($_FILES)){
				echo 'file empty';
				return;
			}
			
			$error_msg = array(
				UPLOAD_ERR_INI_SIZE => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。',
				UPLOAD_ERR_FORM_SIZE => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。',
				UPLOAD_ERR_PARTIAL => '文件只有部分被上传。',
				UPLOAD_ERR_NO_FILE => '没有文件被上传。'
			);
			
			foreach($_FILES as $file){
				$type = gettype($file['name']);
				if($type == 'string'){
					echo move_uploaded_file($file['tmp_name'], DIR_UPLOAD_IMAGE . $file['name']);
				} else if ($type == 'array'){
					$tmps = $file['tmp_name'];
					$fileNames = $file['name'];
					$errors = $file['error'];
					var_dump($errors[0]);
					
					$i = 0;
					foreach($tmps as $tmp){
						if($errors[$i] != 0){
							echo $error_msg[$errors[$i]];
							continue;
						}
						move_uploaded_file($tmp, $fileNames[$i]);
						$i ++;
					}
					
				}
			}
		
		}
	}
}
?>